//
//  EssenceCommentCell.swift
//  FM
//
//  Created by x on 2017/10/13.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class EssenceCommentCell: UITableViewCell {
    
    /// 评论数据模型
    var essenceComment:EssenceComment? {
        didSet {
            //赋值
            iconImage.sd_setImage(with: essenceComment?.user?.avaterURL)
            floorLable.text = "\((essenceComment?.floor)!)" + "楼"
            nickNameLable.text = essenceComment?.user?.nickname
            contentLable.text = essenceComment?.content
            commentTimeLable.text = essenceComment?.created
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    /// 初始化
    func setupUI() -> Void {
        //1.添加子控件
        contentView.addSubview(iconImage)
        contentView.addSubview(floorLable)
        contentView.addSubview(nickNameLable)
        contentView.addSubview(contentLable)
        contentView.addSubview(commentTimeLable)
        contentView.addSubview(reportBtu)
        contentView.addSubview(bgLbale)
        
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        let dics = ["iconImage":iconImage,
                    "floorLable":floorLable,
                    "nickNameLable":nickNameLable,
                    "contentLable":contentLable,
                    "commentTimeLable":commentTimeLable,
                    "reportBtu":reportBtu,
                    "bgLbale":bgLbale] as [String:Any]
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[iconImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[iconImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
 
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImage]-5-[floorLable]", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[floorLable]-5-[nickNameLable]", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImage]-5-[contentLable]-10-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[iconImage]-10-[contentLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[contentLable]-20-[commentTimeLable]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[contentLable]-20-[reportBtu]", options: NSLayoutFormatOptions.alignAllRight, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[bgLbale]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[commentTimeLable]-25-[bgLbale(0.5)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        contentView.addConstraints(cons)
        
    }
    
    /// 举报按钮点击事件
    func reportBtuClick() -> Void {
        SVProgressHUD.show(UIImage.init(named: "")!, status: "举报成功,正在处理...")
    }
    
    /// 根据数据模型计算cell的高度
    ///
    /// - Parameter model: <#model description#>
    /// - Returns: <#return value description#>
    func rowHeight(model:EssenceComment) -> CGFloat {
        essenceComment = model
        layoutIfNeeded()
        
        return bgLbale.frame.maxY
    }
    //MARK: -- 懒加载
    
    /// 头像
    private lazy var iconImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "")
    
    /// 评论楼层
    private lazy var floorLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.RGBColor(r: 180, g: 180, b: 180), fontSize: 14)
    
    /// 评论人姓名
    private lazy var nickNameLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.RGBColor(r: 180, g: 180, b: 180), fontSize: 14)
    
    /// 评论内容
    private lazy var contentLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 14)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        return tt
    }()
    
    /// 评论时间
    private lazy var commentTimeLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.RGBColor(r: 180, g: 180, b: 180), fontSize: 14)
    
    /// 举报按钮
    private lazy var reportBtu:UIButton = UIButton.buttonWith(title: "举报", titleColor: UIColor.RGBColor(r: 180, g: 180, b: 180), fontSize: 14, tag: 0, target: self, action: #selector(reportBtuClick))
    
    private lazy var bgLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 162, g: 162, b: 162))
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
